package br.unisinos.tetris.pieces;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import javax.imageio.ImageIO;

public class FlyweightPiece {

	private static HashMap<Class<Piece>, File> imagePool = new HashMap<Class<Piece>, File>();
	
	public static BufferedImage getPieceImage(Piece piece) throws IOException {
		BufferedImage image = null;
		if (imagePool.containsKey(piece.getClass())) {
			image = ImageIO.read(imagePool.get(piece.getClass()));
		}
		else {
			String imgSrc = "src/br/unisinos/tetris/resources/"+piece.getClass().getSimpleName()+".png";
			File file = new File(imgSrc);
			image = ImageIO.read(file);
			imagePool.put((Class<Piece>)piece.getClass(), file);
		}
		return image;
	}
	
}
